package com.mlizhi.modules.spec;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mlizhi.kingdom.R;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CommonQuestionActivity extends Activity {
	private ImageView back;
	private ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_question);
		
		back = (ImageView) this.findViewById(R.id.global_common_question_back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				CommonQuestionActivity.this.finish();
			}
		});
		
		listView = (ListView)this.findViewById(R.id.dtinfolistview);
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> item = new HashMap<String, Object>();
		item = new HashMap<String, Object>();
		item.put("text1", getString(R.string.help_step1_title));
		StringBuffer sb = new StringBuffer();
		sb.append(getString(R.string.help_step1_msg1));
		sb.append(getString(R.string.help_step1_msg2));
		sb.append(getString(R.string.help_step1_msg3));
		item.put("text2", sb.toString());
		data.add(item);
//		item = new HashMap<String, Object>();
//		item.put("text1", "2.去哪购买mlizhi肌肤水分检测仪？");
//		item.put("text2", "\t\t\t\t点击这里，直接购买产品。");
//		data.add(item);
		item = new HashMap<String, Object>();
		item.put("text1", getString(R.string.help_step2_title));
		sb = new StringBuffer();
		sb.append(getString(R.string.help_step2_msg1));
		sb.append(getString(R.string.help_step2_msg2));
		sb.append(getString(R.string.help_step2_msg3));
		sb.append(getString(R.string.help_step2_msg4));
		sb.append(getString(R.string.help_step2_msg5));
		
		item.put("text2", sb.toString());
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("text1", getString(R.string.help_step3_title));
		item.put("text2", getString(R.string.help_step3_msg));
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("text1", getString(R.string.help_step4_title));
		sb = new StringBuffer();
		sb.append(getString(R.string.help_step4_msg1));
		sb.append(getString(R.string.help_step4_msg2));
		item.put("text2", sb.toString());
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("text1", getString(R.string.help_step5_title));
		sb = new StringBuffer();
		sb.append(getString(R.string.help_step5_msg));
		item.put("text2", sb.toString());
		data.add(item);
		item = new HashMap<String, Object>();
		item.put("text1", getString(R.string.help_step6_title));
		item.put("text2", "");
		data.add(item);
		
		int[] type = {0,0,0,0,0,0,0};
		// 数据与条目界面控件绑定
		MyAdapter adapter = new MyAdapter(this, data, type);
		listView.setAdapter(adapter);
	}
	
	class MyAdapter extends BaseAdapter{ 
		Context mContext; 
		LinearLayout linearLayout = null; 
		LayoutInflater inflater; 
		TextView tex; 
		List<HashMap<String, Object>> data;
		int[] type;
		final int TYPE_1 = 0; 
		final int TYPE_2 = 1; 
		final int TYPE_3 = 2; 
	   
		public MyAdapter(Context context, List<HashMap<String, Object>> data, int[] type) {
			// TODO Auto-generated constructor stub
			mContext = context;
			inflater = LayoutInflater.from(mContext);
			this.data = data;
			this.type = type;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return type.length;
		}

		// 每个convert view都会调用此方法，获得当前所需要的view样式
		@Override
		public int getItemViewType(int position) {
			// TODO Auto-generated method stub
			if (position < 5)
				return TYPE_1;
			else if(position == 5)
				return TYPE_2;
			else
				return TYPE_3;
		}

		@Override
		public int getViewTypeCount() {
			// TODO Auto-generated method stub
			return 3;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return type[arg0];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			viewHolder1 holder1 = null;
			viewHolder2 holder2 = null;
			viewHolder3 holder3 = null;
			int t = getItemViewType(position);

			// 无convertView，需要new出各个控件
			if (convertView == null) {

				// 按当前所需的样式，确定new的布局
				switch (t) {
				case TYPE_1:
					convertView = inflater.inflate(R.layout.view_common_question1, parent, false);
					holder1 = new viewHolder1();
					holder1.textView1 = (TextView) convertView
							.findViewById(R.id.dttext1);
					holder1.textView2 = (TextView) convertView
							.findViewById(R.id.dttext2);
					convertView.setTag(holder1);
					break;
				case TYPE_2:
					convertView = inflater.inflate(R.layout.view_common_question2, parent, false);
					holder2 = new viewHolder2();
					holder2.textView1 = (TextView) convertView
							.findViewById(R.id.dttext3);
					holder2.textView2 = (TextView) convertView
							.findViewById(R.id.dttext4);
					convertView.setTag(holder2);
					break;
				case TYPE_3:
					convertView = inflater.inflate(R.layout.view_common_question3, parent, false);
					holder3 = new viewHolder3();
					holder3.view = (View) convertView
							.findViewById(R.id.dtinfoview);
//					holder3.textView = (TextView) convertView
//							.findViewById(R.id.dttext5);
					convertView.setTag(holder3);
					break;
				}
			} else {
				// 有convertView，按样式，取得不用的布局
				switch (t) {
				case TYPE_1:
					holder1 = (viewHolder1) convertView.getTag();
					break;
				case TYPE_2:
					holder2 = (viewHolder2) convertView.getTag();
					break;
				}
			}

			// 设置资源
			switch (t) {
			case TYPE_1:
				if(position==2) {
//					SpannableString sp = new SpannableString(data.get(position).get("text2").toString());
//			        sp.setSpan(new URLSpan("http://www.i-smooth.com"), 91, 107, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//			        sp.setSpan(new ForegroundColorSpan(Color.rgb(99, 24, 255)), 91, 107, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//			        holder1.textView2.setText(sp);
//			        holder1.textView2.setMovementMethod(LinkMovementMethod.getInstance());
					holder1.textView2.setText(data.get(position).get("text2").toString());
				} else{
					holder1.textView2.setText(data.get(position).get("text2").toString());
				}
				holder1.textView1.setText(data.get(position).get("text1").toString());
				break;
			case TYPE_2:
				holder2.textView1.setText(data.get(position).get("text1").toString());
				break;
			}

			return convertView;
		}
	}

	//各个布局的控件资源 
	class viewHolder1{ 
		TextView textView1; 
		TextView textView2; 
	} 
	   
	class viewHolder2{ 
		TextView textView1; 
		TextView textView2; 
	} 
	
	class viewHolder3{ 
		View view; 
		TextView textView; 
	} 
	
	public void onResume() {
		super.onResume();
		//友盟统计
		MobclickAgent.onResume(this);
	}
	
	public void onPause() {
		super.onPause();
		//友盟统计
		MobclickAgent.onPause(this);
	}
	   
}
